package com.sunlands.zlcx.usercenter.controller;

import com.sunlands.zlcx.usercenter.common.annotation.AuthSig;
import com.sunlands.zlcx.usercenter.common.annotation.AutoLog;
import com.sunlands.zlcx.usercenter.constant.dic.USER_POOL;
import com.sunlands.zlcx.usercenter.domain.DicDO;
import com.sunlands.zlcx.usercenter.domain.UsersDO;
import com.sunlands.zlcx.usercenter.exception.GlobalExceptionHandler;
import com.sunlands.zlcx.usercenter.service.DicServiceImpl;
import com.sunlands.zlcx.usercenter.service.UsersPoolServiceImpl;
import com.sunlands.zlcx.usercenter.vo.UsersVO;
import com.sunlands.zlcx.usercenter.vo.response.BusinessResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import static com.sunlands.zlcx.usercenter.constant.dic.USER_POOL.day_limit;

/**
 * @author 郜炎峰
 * description: 学习目标
 */
@SuppressWarnings("unused")
@Api(tags = "13、用户池（用于推荐用户时使用）", position = 13)
@ApiResponses(value = {@ApiResponse(code = 400, response = GlobalExceptionHandler.class, message = "数据校验失败"), @ApiResponse(code = 500, response = GlobalExceptionHandler.class, message = "内部错误")})
@Slf4j
@RestController
@RequestMapping("/user-pool")
public class UsersPoolController implements AuthSupport {

    @Autowired
    private UsersPoolServiceImpl usersPoolService;

    @Autowired
    private DicServiceImpl dicService;

    @ApiOperation(value = "学习时长较长用户排行榜")
    @GetMapping("/learn-time-top-users")
    @AutoLog
    @PreAuthorize("isAuthenticated()")
    @AuthSig
    public BusinessResult<List<UsersVO>> learnTimeTopUsers(@ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") Integer page,
                                                           @ApiParam(value = "条数") @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        DicDO code = dicService.findByCode(USER_POOL.dicName());
        Long dayLimit = code.filter(day_limit.name()).getCode();

        //获取dayLimit天内学习时长最高的用户
        Calendar startTime = Calendar.getInstance();
        startTime.setTime(new Date());
        startTime.set(Calendar.HOUR, startTime.get(Calendar.HOUR) - (int) (dayLimit * 24));

        //最近8小时数据不进行计算，防止时间差导致本地缓存数据不一致
        Calendar endTime = Calendar.getInstance();
        endTime.setTime(new Date());
        endTime.set(Calendar.HOUR, endTime.get(Calendar.HOUR) - 8);

        List<UsersDO> users = usersPoolService.learnTimeTopUsers(page, pageSize, startTime.getTime(), endTime.getTime());
        List<UsersVO> result = users.stream().map(UsersVO::from).collect(Collectors.toList());

        return BusinessResult.createSuccessInstance(result);
    }


}
